package com.zhou.yygh.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.zhou.yygh.cmn.client.DictFeignClient;
import com.zhou.yygh.hosp.repository.HospitalRepository;
import com.zhou.yygh.hosp.service.HospitalService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;
import zhou.hospital.model.hosp.Hospital;
import zhou.hospital.model.hosp.HospitalSet;
import zhou.hospital.vo.hosp.HospitalQueryVo;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class HospitalServiceImpl implements HospitalService {


    @Autowired
   private DictFeignClient dictFeignClient;
    @Autowired
    HospitalRepository hospitalRepository;

    //获得医院信息
    @Override
    public Hospital getHospital(String hoscode) {
        Hospital hospital = hospitalRepository.findHospitalByHoscode(hoscode);
        return hospital;
    }

    // 上传医院信息
    @Override
    public void save(Map<String, Object> paramMap) {
        String mapString = JSONObject.toJSONString(paramMap);
        Hospital hospital = JSONObject.parseObject(mapString, Hospital.class);
        Hospital hospitalExist = hospitalRepository.findHospitalByHoscode(hospital.getHoscode());
        if (hospitalExist != null) {
            hospital.setStatus(hospital.getStatus());
            hospital.setCreateTime(hospitalExist.getCreateTime());
            hospital.setUpdateTime(new Date());
            hospital.setIsDeleted(0);
            hospitalRepository.save(hospital);
        } else {
            hospital.setCreateTime(new Date());
            hospital.setUpdateTime(new Date());
            hospital.setIsDeleted(0);
            hospital.setStatus(1);
            hospitalRepository.save(hospital);
        }
    }

    //条件分页查询所有医院信息
    @Override
    public Page<Hospital> selectHospPage(Integer page, Integer limit, HospitalQueryVo hospitalQueryVo) {
        Hospital hospital = new Hospital();
        BeanUtils.copyProperties(hospitalQueryVo,hospital);
        Pageable pageable =  PageRequest.of(page-1,limit);
        ExampleMatcher matcher =ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase(true);
        Example<Hospital> example = Example.of(hospital,matcher);
        Page<Hospital> pages = hospitalRepository.findAll(example, pageable);

        //调用service-cmn中的接口获取医院等级等信息
        pages.getContent().stream().forEach(item ->{
            this.setHospitalHosType(item);
        });



        return  pages;
    }

    // 从 dict表中查询医院的其他信息，包括所在地，医院等级
    private Hospital setHospitalHosType(Hospital hospital) {
        Map<String, Object> param = hospital.getParam();

        //查询医院等级
        String hosTypeString = dictFeignClient.getName(hospital.getHostype());

        //查询省市区
        String provinceString = dictFeignClient.getName(hospital.getProvinceCode());
        String cityString = dictFeignClient.getName(hospital.getCityCode());
        String districtString = dictFeignClient.getName(hospital.getDistrictCode());

       param.put("hosTypeString",hosTypeString);
       param.put("fullAddress",provinceString+cityString+districtString);
       return hospital;
    }

    //更新医院状态
    @Override
    public void updateHospStatus(String id, Integer status) {
        Hospital hospital = hospitalRepository.findById(id).get();
        hospital.setStatus(status);
        hospital.setUpdateTime(new Date());
        hospitalRepository.save(hospital);
    }

    //查询医院详情
    @Override
    public Map<String,Object> getHospitalById(String id) {
        HashMap<String, Object> map = new HashMap<>();
        Hospital hospital = hospitalRepository.findById(id).get();
        Hospital hospitalAll = this.setHospitalHosType(hospital);
        map.put("hospital",hospitalAll);

        //医院预约信息
        map.put("bookingRule",hospitalAll.getBookingRule());
        hospital.setBookingRule(null);
        return map;
    }

    @Override
    public String getHospName(String hoscode) {
        Hospital hospital = this.getHospital(hoscode);
        if(hospital!=null){
            return hospital.getHosname();
        }
        return null;
    }

    @Override
    public List<Hospital> findHosByName(String hosname) {
       List<Hospital> list =  hospitalRepository.findHospitalByHosnameLike(hosname);
       return  list;
    }


    //根据hosname获取医院预约挂号详情
    @Override
    public Map<String, Object> items(String hoscode) {
        HashMap<String, Object> result = new HashMap<>();
        Hospital hospital1 = this.setHospitalHosType(this.getHospital(hoscode));
        result.put("hospital",hospital1);
        result.put("bookingRule",hospital1.getBookingRule());
        hospital1.setBookingRule(null);
        return result;



    }
}
